package com.coincalf.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.coincalf.entity.WorkIssue;
import com.coincalf.framework.enums.WorkIssueStatus;
import com.coincalf.framework.exception.GlobalDefaultException;
import com.coincalf.framework.http.Response;
import com.coincalf.framework.security.UserDetails;
import com.coincalf.service.WorkIssueService;
import com.coincalf.vo.WorkIssueForm;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.Optional;

/**
 * <p>
 * 工单记录 前端控制器
 * </p>
 *
 * @author qiang
 * @since 2018-05-31
 */
@RestController
@RequestMapping("/workIssue")
@Slf4j
@Api(value = "工单系统", description = "工单系统 REST API")
public class WorkIssueController {

    @Autowired
    private WorkIssueService workIssueService;
    @Autowired
    private CommonController commonController;


    @PostMapping("/addWorkIssue")
    @ApiOperation(value = "添加工单", notes = "添加工单", httpMethod = "POST")
    //@PreAuthorize("isAuthenticated()")
    public Object addWorkIssue(HttpServletRequest request, @RequestBody @Valid WorkIssueForm workIssueForm) {
        Long userId = commonController.getUserId(request);
        WorkIssue workIssue = new WorkIssue();
        workIssue.setQuestion(workIssueForm.getQuestion()).
                setUserId(userId).
                setStatus(WorkIssueStatus.NOT_AN.getCode()).
                setCreated(new Date()).
                setLastUpdateTime(new Date());
        boolean flag = workIssueService.insert(workIssue);
        if (!flag) {
            throw new GlobalDefaultException(50053);
        }
        return Response.ok();
    }

    @GetMapping("/issueList/{current}/{size}")
    @ApiOperation(value = "获取工单列表", notes = "获取工单列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "size", value = "每页显示数据量", required = true, dataType = "int", paramType = "path")
    })
    //@PreAuthorize("isAuthenticated()")
    public Object issueList(
            HttpServletRequest request,
            @PathVariable("current") int current,
            @PathVariable("size") int size
    ) {
        Long userId = commonController.getUserId(request);
        EntityWrapper<WorkIssue> e = new EntityWrapper<>();
        e.eq("user_id", userId);
        e.orderBy("created", false);
        Page<WorkIssue> page = new Page<>(current, size);
        return Response.ok(workIssueService.selectPage(page, e));
    }

}

